package com.tinysand.advert.index;

import com.alibaba.fastjson.JSON;
import com.tinysand.advert.constant.DataConstant;
import com.tinysand.advert.handler.AdLevelDataHandler;
import com.tinysand.advert.mysql.constant.OperationType;
import com.tinysand.advert.support.*;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 索引文件加载器，实现对所有索引数据的加载
 *
 * Created By TINY
 * Create Time 2020/2/27
 **/
@Component
@DependsOn("dataTable")
public class IndexFileLoader {
    @PostConstruct
    public void init() {
        List<String> adPlanStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_PLAN)
        );
        adPlanStrings.forEach(p -> AdLevelDataHandler.handleLevel2(
                JSON.parseObject(p, AdPlanTable.class),
                OperationType.ADD
        ));

        List<String> advertStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_ADVERT)
        );
        advertStrings.forEach(a -> AdLevelDataHandler.handleLevel2(
                JSON.parseObject(a, AdvertTable.class),
                OperationType.ADD
        ));

        List<String> adUnitStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_UNIT)
        );
        adUnitStrings.forEach(u -> AdLevelDataHandler.handleLevel3(
                JSON.parseObject(u, AdUnitTable.class),
                OperationType.ADD
        ));

        List<String> adUnitRelStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_UNIT_REL)
        );
        adUnitRelStrings.forEach(a -> AdLevelDataHandler.handleLevel3(
                JSON.parseObject(a, AdUnitRelTable.class),
                OperationType.ADD
        ));

        List<String> adDistrictStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_UNIT_DISTRICT)
        );
        adDistrictStrings.forEach(d -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(d, DistrictTable.class),
                OperationType.ADD
        ));

        List<String> adInterestStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_UNIT_INTEREST)
        );
        adInterestStrings.forEach(i -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(i, InterestTable.class),
                OperationType.ADD
        ));

        List<String> adKeywordStrings = loadDumpData(
                String.format("%s%s",
                        DataConstant.DATA_ROOT_DIR, DataConstant.AD_UNIT_KEYWORD)
        );
        adKeywordStrings.forEach(k -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(k, KeywordTable.class),
                OperationType.ADD
        ));
    }

    private List<String> loadDumpData(String fileName) {
        try (BufferedReader reader = Files.newBufferedReader(Paths.get(
                fileName))) {

            return reader.lines().collect(Collectors.toList());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
